package com.woniuxy.movie.commons.core.util;

import com.woniuxy.movie.commons.core.exception.TokenException;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;

import javax.crypto.SecretKey;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class JwtUtil {

    private static final String adminSecret = "jlsdjflsjflkjsdlfsdkldfnklsdnflsdnlksdkf";
    private static final String userSecret = "jlsdjfksdninfghfdgnkjnsuihfjsammjiaakiakaakakk";

    /**
     * 生成Token
     *
     * @param body
     * @param time 秒
     * @param secret
     * @return
     */
    public static String createToken(Map<String, Object> body, Integer time, String secret) {
        SecretKey key = Keys.hmacShaKeyFor(secret.getBytes());
        JwtBuilder jb = Jwts.builder();
        if (body == null || body.size() <= 0) {
            throw new TokenException(5001, "没有数据");
        }
        jb.setClaims(body);
        if (time == null) {
            jb.setExpiration(new Date(System.currentTimeMillis() + 60 * 30 * 1000));
        } else {
            jb.setExpiration(new Date(System.currentTimeMillis() + time * 1000));
        }
        return jb.signWith(key).compact();
    }

    /**
     * 生成Token
     *
     * @param body
     * @param secret
     * @return
     */
    public static String createToken(Map<String, Object> body, String secret) {
        return createToken(body, null, secret);
    }

    /**
     * 解析
     *
     * @param token
     * @param secret
     * @return
     */
    public static Claims parseToken(String token, String secret) throws Exception {
        if (token == null) {
            throw new TokenException(5002, "token为空");
        }
        SecretKey key = Keys.hmacShaKeyFor(secret.getBytes());
        return Jwts.parserBuilder().setSigningKey(key).build().parseClaimsJws(token).getBody();
    }

    /**
     * 生成User端Token
     *
     * @param body
     * @return
     */
    public static String createUserToken(Map<String, Object> body) {
        return createToken(body, userSecret);
    }

    /**
     * 生成User端Token
     *
     * @param body
     * @return
     */
    public static String createUserToken(Object... body) {
        if (body == null || body.length <= 0) {
            throw new TokenException(5003, "没有主体数据");
        }
        Map<String, Object> maps = new HashMap<>();
        for (int i = 0; i < body.length; i += 2) {
            Object value = null;
            if (i + 1 < body.length) {
                value = body[i + 1];
            }
            maps.put(body[i].toString(), value);
        }
        return createToken(maps, userSecret);
    }

    /**
     * 生成admin端Token
     *
     * @param body
     * @return
     */
    public static String createAdminToken(Map<String, Object> body) {
        return createToken(body, adminSecret);
    }

    /**
     * 生成Admin端Token
     *
     * @param body
     * @return
     */
    public static String createAdminToken(Object... body) {
        if (body == null || body.length <= 0) {
            throw new TokenException(5003, "没有主体数据");
        }
        Map<String, Object> maps = new HashMap<>();
        for (int i = 0; i < body.length; i += 2) {
            Object value = null;
            if (i + 1 < body.length) {
                value = body[i + 1];
            }
            maps.put(body[i].toString(), value);
        }
        return createToken(maps, adminSecret);
    }

    /**
     * 解析User端Token
     *
     * @param token
     * @return
     */
    public static Claims parseUserToken(String token) throws Exception {
        return parseToken(token, userSecret);
    }

    /**
     * 解析Admin端Token
     *
     * @param token
     * @return
     */
    public static Claims parseAdminToken(String token) throws Exception {
        return parseToken(token, adminSecret);
    }


}
